from socket import gethostbyname
from time import sleep
from re import compile

pattern = compile(
    r'^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|'
    r'([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|'
    r'([a-zA-Z0-9][-_.a-zA-Z0-9]{0,61}[a-zA-Z0-9]))\.'
    r'([a-zA-Z]{2,13}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3})$'
)


def domain_check(url):
    result_list = []
    with open(file="../dict/subdomain.txt", mode="r", encoding="utf-8") as file:
        if pattern.match(url):
            url = url.replace('www.', '')
            for domain_data in file:
                domain_data = domain_data.replace('\n', '')
                domain_url = domain_data + '.' + url
                try:
                    ip = gethostbyname(domain_url)
                    result_list.append(f"""{domain_url} >>> {ip}""")
                except Exception:
                    sleep(0.1)
            return result_list
        else:
            result_list.append(f"""输入不正确，输入示例：www.scitc.com.cn""")
            return result_list
